﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Managers;

namespace GameFramework.Battle.Core
{
    public class MaterialManager
    {
        private Dictionary<int, Material> m_dic = new Dictionary<int, Material>();
        private static MaterialManager m_instance;
        public static MaterialManager GetInstance()
        {
            if (m_instance != null) return null;
            return m_instance = new MaterialManager();
        }
        MaterialManager()
        {
//            BattleProxy.ALL_MANAGER_COUNTER++;
        }
        ~MaterialManager()
        {
//            BattleProxy.ALL_MANAGER_COUNTER--;
        }

        public void Initialize(Dictionary<string, string> materialDic,Action callBack)
        {
            if (materialDic == null || materialDic.Count <= 0) return;
            int count = materialDic.Count;
            if (count <= 0) return;
            foreach (var item in materialDic)
            {
                var value = item.Value;
                var key = item.Key;
                var type = Enum.Parse(typeof(EMaterialType), key);
                Shader o= ShaderMgr.Find(value);
                if (o!=null)
                {
                    var s = o as Shader;
                    if (s.isSupported && SystemInfo.supportsImageEffects)
                    {
                        m_dic.Add((int)type, new Material(s));
                    }
                    count--;
                    if (count == 0 && callBack != null)
                        callBack();
                }
                else
                {

                }
            }
        }
        public void Dispose()
        {
            //foreach (var item in m_dic)
            //{
            //    External.ExternalManager.UnLoadAsset(External.EAssetType.Shader, item.Value, "");
            //}
            m_dic.Clear();
            m_instance = null;
        }
        public static Material Get(EMaterialType type)
        {
            if (m_instance.m_dic.ContainsKey((int)type))
            {
                return m_instance.m_dic[(int)type];
            }
            return null;
        }
    }
}
